package common;

import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.w3c.dom.Element;

import fitness.AmplyFitness;
import fitness.Fitness;

import optimizers.commons.RunConfig;
import optimizers.ganeat.Constants;
import optimizers.ganeat.GA;
import optimizers.ganeat.amplify.ConstantsAmply;
import optimizers.ganeat.amplify.GAAmplify;

public class RunAmplify extends RunStarter {

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Amplify";
	}

	@Override
	public HashMap<String, String> getDefaultParameters() {
		HashMap<String, String> parameters = new HashMap<String, String>();
		parameters.put("Amplitude Input",
				String.valueOf(ConstantsAmply.amplitudeInput));
		parameters.put("Pulse Duration",
				String.valueOf(ConstantsAmply.pulseDuration));
		parameters.put("nb Rounds", String.valueOf(ConstantsAmply.nbRounds));
		parameters.put("Min Down Time",
				String.valueOf(ConstantsAmply.minDowntime));
		parameters.put("Max Down Time",
				String.valueOf(ConstantsAmply.maxDowntime));
		return parameters;
	}

	@Override
	public void setParameters(HashMap<String, String> parameters) {
		try {
			ConstantsAmply.amplitudeInput = Double.parseDouble(parameters
					.get("Amplitude Input"));
			ConstantsAmply.pulseDuration = Integer.parseInt(parameters
					.get("Pulse Duration"));
			ConstantsAmply.nbRounds = Integer.parseInt(parameters
					.get("nb Rounds"));
			ConstantsAmply.minDowntime = Integer.parseInt(parameters
					.get("Min Down Time"));
			ConstantsAmply.maxDowntime = Integer.parseInt(parameters
					.get("Max Down Time"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	public void start() {
		runStatus.status = runStatus.RUNNING;
		for (int i = 0; i < Constants.nRuns; i++) {
			GAAmplify ga = (GAAmplify) getGA();
			try {
				ga.start();
			} catch (CloneNotSupportedException | InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public Fitness getFitnessFunction() {
		// TODO Auto-generated method stub
		return new AmplyFitness();
	}

	@Override
	public GA getGA() {
		// TODO Auto-generated method stub
		return new GAAmplify(config, initIndiv, panelFitness, panelSpecies,
				panelIndiv, runStatus, progressBar, fitnessElement);
	}
}
